package com.suneast.suneaststore.modules.search.adapter;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.search.SearchBean;

public class SearchBeanAdapter extends BaseQuickAdapter<SearchBean, SearchBeanAdapter.ViewHolder> {

    public SearchBeanAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(ViewHolder helper, SearchBean item) {
        Glide.with(helper.itemView.getContext())
                .load(item.getThumb()).into((ImageView) helper.getView(R.id.img_product));
        helper.getView(R.id.tv_choice)
                .setVisibility(item.getMrecommand() == 1 ? View.VISIBLE : View.GONE);
        helper.setText(R.id.tv_product_name, item.getTitle());
        Spannable memberPriceSpan = new SpannableString(String.format(
                helper.itemView.getResources().getString(R.string.search_total_price),
                item.getMemberprice(), item.getUnit()));
        memberPriceSpan.setSpan(new AbsoluteSizeSpan(40), 0, item.getMemberprice().length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        memberPriceSpan.setSpan(new ForegroundColorSpan(Color.RED), 0,
                item.getMemberprice().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.tv_total_price, memberPriceSpan);
        Double dou = Double.valueOf(item.getDiscount_price());

        if (dou != null && dou > 0) {
            Spannable multiplePriceSpan = new SpannableString(String.format(
                    helper.itemView.getResources().getString(R.string.search_multiple_price),
                    item.getReal_price(), item.getUnit(), item.getDiscount_price(), item.getUnit()));
            multiplePriceSpan.setSpan(new AbsoluteSizeSpan(40), 0, item.getReal_price().length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            multiplePriceSpan.setSpan(new ForegroundColorSpan(Color.RED), 0,
                    item.getReal_price().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            multiplePriceSpan.setSpan(new AbsoluteSizeSpan(40), item.getReal_price().length() + 5,
                    item.getReal_price().length() + 7 + item.getDiscount_price().length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            multiplePriceSpan.setSpan(new ForegroundColorSpan(Color.RED), item.getReal_price().length() + 5,
                    item.getReal_price().length() + 7 + item.getDiscount_price().length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.tv_multiple_price, multiplePriceSpan);
        } else {
            helper.getView(R.id.tv_multiple_price).setVisibility(View.INVISIBLE);
        }
        if (item.getCoin_reward() >= 100d) {
            helper.getView(R.id.tv_reward).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_reward, "按支付现金返等额帘邦豆");
        } else if (item.getCoin_reward() > 0) {
            helper.getView(R.id.tv_reward).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_reward, "按支付现金返" + (int) item.getCoin_reward() + "%联邦豆");
        } else {
            helper.getView(R.id.tv_reward).setVisibility(View.GONE);
        }

    }

    public static class ViewHolder extends BaseViewHolder {

        public ViewHolder(View view) {
            super(view);
        }
    }
}
